$(function () {
      var res = null;
      let usermsg = JSON.parse(localStorage.getItem("usermsg"));
      if (usermsg !== null) {
          writeUserMsg();
      }
  
      function writeUserMsg() {
          $("#username").val(usermsg.username);
          $("#password").val(usermsg.password);
  
      }
  
      function login() {
          let options = {
              headers: {
                  "Content-Type": "application/x-www-form-urlencoded",
              }
          }
          let data = {
              username: $("#username").val(),
              password: $("#password").val(),
          }
          axios.post("http://127.0.0.1:8888/users/login", formatData(data), options)
              .then(response => {
                  console.log(response.data);
                  console.log(response);

                  console.log(formatData(data));

                  res = response.data;
                  if (res.code === 0) {
                      error(res);
                  } else if (res.code === 1) {
                      success(res);
                  }
              })
      }
  
      $("#login-btn").click(() => login());
  
      function error(res) {
          alert("抱歉登录失败 : " + res.message);
      }
  
      function success(res) {
          alert("恭喜登陆成功！");
          location.href = "./index.html";
          cookie("Token", res.token);
          cookie("ID", res.user.id);
      }
  })
